package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.PslcddyDto;
import org.jeecg.modules.demo.hydrology.dto.PslcdssDto;
import org.jeecg.modules.demo.hydrology.entity.Pslcddy;
import org.jeecg.modules.demo.hydrology.entity.Pslcdss;
import org.jeecg.modules.demo.hydrology.mapper.PslcddyMapper;
import org.jeecg.modules.demo.hydrology.mapper.PslcdssMapper;
import org.jeecg.modules.demo.hydrology.service.IPslcdssService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 排水量实时
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class PslcdssServiceImpl extends ServiceImpl<PslcdssMapper, Pslcdss> implements IPslcdssService {
    @Autowired
    private PslcdssMapper pslcdssMapper;
    @Autowired
    private PslcddyMapper pslcddyMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 排水量实时-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<PslcdssDto> getList(Page<Pslcdss> page, QueryWrapper<Pslcdss> queryWrapper) {
        Page<Pslcdss> pslcdssPage = pslcdssMapper.selectPage(page, queryWrapper);
        List<PslcdssDto> collect = pslcdssPage.getRecords().stream().map(pslcdss -> {
            PslcdssDto pslcdssDto = new PslcdssDto();
            BeanUtils.copyProperties(pslcdss, pslcdssDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", pslcdss.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                pslcdssDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","point_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",pslcdss.getStationStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    pslcdssDto.setPointStatusItemText(sysDictItem.getItemText());
                }
            }
            QueryWrapper<Pslcddy> pslcddyQueryWrapper = new QueryWrapper<>();
            pslcddyQueryWrapper.eq("station_code",pslcdss.getStationCode());
            Pslcddy pslcddy = pslcddyMapper.selectOne(pslcddyQueryWrapper);
            if (pslcddy != null){
                pslcdssDto.setLocation(pslcddy.getEquipmentPosition());
            }
            return pslcdssDto;
        }).collect(Collectors.toList());
        IPage<PslcdssDto> pslcdssDtoIPage = new Page<>();
        pslcdssDtoIPage.setRecords(collect);
        pslcdssDtoIPage.setTotal(pslcdssPage.getTotal());
        pslcdssDtoIPage.setPages(page.getPages());
        pslcdssDtoIPage.setSize(page.getSize());
        return pslcdssDtoIPage;
    }
}
